package com.hxc.toolslibrary.http.Interceptor

import com.gxcommunication.sociallearn.utils.CommonSharePreferenceUtils
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author chenxz
 * @date 2018/9/26
 * @desc HeaderInterceptor: 设置请求头
 */
class HeaderInterceptor : Interceptor {

    /**
     * token
     */
    private var token: String = ""

    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()
        val builder = request.newBuilder()

        builder.addHeader("client", "android")
                .addHeader("Content-Type", "application/json;charset=UTF-8")
//                .addHeader(addHeader"version", AppUtils.getAppVersionName() + "")

        if (CommonSharePreferenceUtils.getUserToken().isNotBlank()){
            builder.addHeader("Auth-Token", CommonSharePreferenceUtils.getUserToken() )
        }


//            .addHeader("Content-type", "application/json; charset=utf-8")

//        val domain = request.url().host()
//        val url = request.url().toString()
//        if (domain.isNotEmpty() && (url.contains(HttpConstant.COLLECTIONS_WEBSITE)
//                        || url.contains(HttpConstant.UNCOLLECTIONS_WEBSITE)
//                        || url.contains(HttpConstant.ARTICLE_WEBSITE)
//                        || url.contains(HttpConstant.TODO_WEBSITE)
//                        || url.contains(HttpConstant.COIN_WEBSITE))) {
//            val spDomain: String by Preference(domain, "")
//            val cookie: String = if (spDomain.isNotEmpty()) spDomain else ""
//            if (cookie.isNotEmpty()) {
//                // 将 Cookie 添加到请求头
//                builder.addHeader(HttpConstant.COOKIE_NAME, cookie)
//            }
//        }

        return chain.proceed(builder.build())
    }

}